﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Drawing;

public partial class Shipper : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection();
    protected void Page_Load(object sender, EventArgs e)
    {    
        con.ConnectionString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        if (this.IsPostBack == false)
        {
            LoadShipperInfo();
            DefaultShipperForm();
            btnAdd.Enabled = true;
            btnUpdate.Enabled = false;
            btnDelete.Enabled = false;
            Session["Shipper_Search"] = false;
        }
    }


    #region Code Shipper
    void InsertNewShipper()
    {
        try
        {
            con.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "Sales.Shippers_Insert";
            SqlParameter param = new SqlParameter("@Companyname", SqlDbType.NVarChar, 40);
            param.Value = txtShipperCompany.Text;
            cmd.Parameters.Add(param);
            param = new SqlParameter("@phone", SqlDbType.NVarChar, 40);
            param.Value = txtShipperPhone.Text;
            cmd.Parameters.Add(param);
            cmd.ExecuteNonQuery();
            checksearch();
            Response.Write("<script language='JavaScript'> alert('Insert successful!'); </script>");
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            con.Close();
        }

    }

    void DeleteShipperInfo()
    {
        if (txtShipperid.Text != "")
        {
            try
            {
                con.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = con;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "Sales.Shippers_Delete";
                SqlParameter param = new SqlParameter("@Shipperid", SqlDbType.Int);
                param.Value = int.Parse(txtShipperid.Text);
                cmd.Parameters.Add(param);
                cmd.ExecuteNonQuery();
                Response.Write("<script language='JavaScript'> alert('Delete successful!'); </script>");
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                con.Close();
            }

            checksearch();
        }

    }

    void UpdateShipperInfo()
    {
        try
        {
            con.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "Sales.Shippers_Update";
            SqlParameter param = new SqlParameter("@Shipperid", SqlDbType.Int);
            param.Value = int.Parse(txtShipperid.Text);
            cmd.Parameters.Add(param);
            param = new SqlParameter("@Companyname", SqlDbType.NVarChar, 40);
            param.Value = txtShipperCompany.Text;
            cmd.Parameters.Add(param);
            param = new SqlParameter("@phone", SqlDbType.NVarChar, 40);
            param.Value = txtShipperPhone.Text;
            cmd.Parameters.Add(param);
            cmd.ExecuteNonQuery();
            Response.Write("<script language='JavaScript'> alert('Update successful!'); </script>");
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            con.Close();
        }
        checksearch();


    }

    void DefaultShipperForm()
    {
        txtShipperid.Text = "";
        txtShipperCompany.Text = "";
        txtShipperPhone.Text = "";
    }

    void LoadShipperInfo()
    {
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "Sales.Shippers_get";
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        gvShipper.AutoGenerateSelectButton = true;
        gvShipper.DataSource = dt;
        gvShipper.DataBind();
    }

    void searchShipper(string search)
    {
        if (txtShipperSearch.Text.Length == 0)
        {
            Session["Shipper_Search"] = false;
            LoadShipperInfo();
            DefaultShipperForm();
        }
        else
        {
            LoadShipperInfo();
            DefaultShipperForm();
            GridViewRow dr;
            bool show;
            for (int i = 0; i < gvShipper.Rows.Count; i++)
            {
                show = false;
                dr = gvShipper.Rows[i];
                dr.Visible = true;
                dr.Cells[2].BackColor = Color.White;
                if (dr.Cells[2].Text.ToLower().Contains(search.ToLower()) == true)
                {
                    dr.Cells[2].BackColor = Color.Yellow;
                    show = true;
                }
                if (show == false)
                {
                    dr.Visible = false;
                }
            }
        }
    }

    void checksearch()
    {
        if ((bool)Session["Shipper_Search"] == true)
        {
            gvShipper.DataBind();
            searchShipper(Session["Shipper_searchString"].ToString());
        }
        else
        {
            LoadShipperInfo();
        }
    } 
    #endregion

    #region Action Shipper
    protected void btnNew_Click(object sender, EventArgs e)
    {
        Session["Order_Search"] = false;
        checksearch();
        DefaultShipperForm();
        btnAdd.Enabled = true;
        btnUpdate.Enabled = false;
        btnDelete.Enabled = false;
        txtShipperid.Enabled = false;
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        checksearch();
        InsertNewShipper();
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        UpdateShipperInfo();
        DefaultShipperForm();
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        try
        {
            DeleteShipperInfo();
            DefaultShipperForm();
        }
        catch (Exception)
        {
            Response.Write("<script language='JavaScript'> alert('Some data is used! You can not delete this shipper'); </script>");
        }
    }
    protected void btnShipperSearch_Click(object sender, EventArgs e)
    {
        Session["Shipper_Search"] = true;
        Session["Shipper_searchString"] = txtShipperSearch.Text;
        searchShipper(Session["Shipper_searchString"].ToString());
    }
    protected void gvShipper_SelectedIndexChanged(object sender, EventArgs e)
    {
        btnAdd.Enabled = false;
        btnDelete.Enabled = true;
        btnUpdate.Enabled = true;
        txtShipperid.Enabled = true;
        GridViewRow dg = gvShipper.SelectedRow;
        txtShipperid.Text = Server.HtmlDecode( dg.Cells[1].Text.Trim());
        txtShipperCompany.Text = Server.HtmlDecode( dg.Cells[2].Text);
        txtShipperPhone.Text = Server.HtmlDecode( dg.Cells[3].Text);
    } 
    #endregion
}
